package com.zw.pdm.module.file.service;

import com.zw.pdm.constant.Constant;
import com.zw.pdm.entity.production.FileSheet;
import com.zw.pdm.entity.rep.VerifyProcess;
import com.zw.pdm.module.file.mapper.FileCheckMapper;
import com.zw.pdm.module.file.model.FileSheetModel;
import com.zw.pdm.module.file.repository.FileSheetRepository;
import com.zw.pdm.module.report.mapper.VerifyProcessMapper;
import com.zw.pdm.module.report.repository.VerifyProcessRepository;
import com.zw.pdm.module.system.service.LogService;
import com.zw.pdm.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.zw.pdm.util.BeanUtil.copy;

/**
 * @author mayongfeng
 * @version 1.0.0
 * @ClassName FileCheckService.java
 * @Description TODO
 * @createTime 2020年12月15日 10:07:00
 */
@Service
@SuppressWarnings("all")
public class FileCheckService {

    @Autowired
    private FileCheckMapper fileCheckMapper;
    @Autowired
    private VerifyProcessMapper verifyProcessMapper;
    @Autowired
    private FileSheetRepository fileSheetRepository;
    @Autowired
    private VerifyProcessRepository verifyProcessRepository;
    @Autowired
    private LogService logService;

    /**加载存档清单待审核列表功能*/
    public List<FileSheetModel> getFileCheckList(FileSheetModel fileSheet) {
        fileSheet.setVerifier(ShiroUtils.getUserId());
        return fileCheckMapper.getFileCheckList(fileSheet);

    }

    /**
     * @Author mayongfeng
     * @Date 15:12 2020/12/15
     * @Description 存档清单审核
     *
     **/
    @Transactional(rollbackFor = Exception.class)
    public int fileVerify(Integer id) {
        VerifyProcess vp = verifyProcessMapper.findCurVpByFileSheetId(id);
        if (vp.getComp() == 1) {
            FileSheet fileSheet  = fileSheetRepository.getOne(id);
            fileSheet.setStatus(Constant.STATUS_COMPLETE);
            fileSheetRepository.saveAndFlush(fileSheet);
            return verifyProcessMapper.updateStatusAndCurAndTimeById(vp.getId(), 2, 2, new Date());
        } else {
            // 修改下一个审核节点为当前节点
            VerifyProcess nextVp = verifyProcessMapper.findFileNextVp(vp);
            nextVp.setCur(1);
            verifyProcessRepository.saveAndFlush(nextVp);
        }
        logService.saveLog("审核存档清单", "清单ID：" + vp.getFileSheetId());
        return verifyProcessMapper.updateStatusAndCurAndTimeById(vp.getId(), 2, 0, new Date());

    }

    /** 驳回计划 */
    @Transactional(rollbackFor = Exception.class)
    public int fileReject(FileSheet sheet) {
        VerifyProcess vp = verifyProcessMapper.findCurVpByFileSheetId(sheet.getId());
        logService.saveLog("驳回存档清单Id", "存单清单ID：" + vp.getFileSheetId());
        FileSheet fileSheet = fileSheetRepository.getOne(sheet.getId());
        copy(sheet, fileSheet);
        if (vp.getStart() == 1) {
            // 如果为第一个审核节点，修改计划为未提交，删除所有审核结点
            fileSheet.setStatus(Constant.STATUS_REJECT);
            fileSheet.setCreateTime(new Date());
            fileSheetRepository.saveAndFlush(fileSheet);
            return verifyProcessMapper.delByFileSheetId(sheet.getId());
        } else {
            // 修改上一个审核节点为当前节点，并标识为驳回状态
            fileSheetRepository.saveAndFlush(fileSheet);
            VerifyProcess preVp = verifyProcessMapper.findFilePreVp(vp);
            preVp.setCur(1);
            preVp.setStatus(0);
            verifyProcessRepository.saveAndFlush(preVp);
            return verifyProcessMapper.updateFileStatusAndCurAndTimeById(vp.getId(), 1, 0, null);
        }
    }


    public VerifyProcess editVerify(VerifyProcess verifyProcess) {
        VerifyProcess vp = verifyProcessRepository.getOne(verifyProcess.getId());
        VerifyProcess nextVp = verifyProcessMapper.findFileNextVp(vp);
        nextVp.setVerifier(verifyProcess.getVerifier());
        logService.saveLog("修改审核人", "");
        return verifyProcessRepository.saveAndFlush(nextVp);
    }
}
